package services;

import http.AlphabeticParserStrategy;
import http.CaptchaForm;
import http.CaptchaPageParserStrategy;
import http.CategoryParserStrategy;
import http.DownloadPageParserStrategy;
import http.Link;
import http.Page;
import http.ParserConfig;
import http.SeasonParserStrategy;
import http.SeriesParserStrategy;
import http.WelcomeParserStrategy;

import java.io.IOException;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Map.Entry;

public class CategoryService {
	private static CategoryService instance = null;
	private CategoryService() {
	}
	public static CategoryService getInstance() {
		if (instance == null)
			instance = new CategoryService();
		return instance;
	}
	
	public Map<String,String> getAlphabethics() {
		Page page = null;
		Map<String,String> alphas = new LinkedHashMap<String,String>();
		AlphabeticParserStrategy alpha = null;
		for (int i = 0;i < 10 && (alpha == null || !alpha.isParseable());i++) {
			try {
				page = new Page(ParserConfig.getInstance().getConfig("startpage"));
				WelcomeParserStrategy welcome = new WelcomeParserStrategy();
				alpha = new AlphabeticParserStrategy();
				page.addParser(welcome);
				page.addParser(alpha);
				page.open();
				if (welcome.isParseable())
					page.open();
				for (Entry<String,Link> e:alpha.getLinks().entrySet()) {
					alphas.put(e.getValue().toString(), e.getKey());
				}
			} catch (IOException e1) {
				e1.printStackTrace();
			}
		}
		return alphas;
	}
	public Map<String,String> getCategories(String link) {
		Map<String,String> categories = new LinkedHashMap<String,String>();
		CategoryParserStrategy category = null;
		for (int i = 0;i < 10 && (category == null || !category.isParseable());i++) {
			try {
				Page page = new Page(link);
				WelcomeParserStrategy welcome = new WelcomeParserStrategy();
				category = new CategoryParserStrategy();
				page.addParser(welcome);
				page.addParser(category);
				page.open();
				if (welcome.isParseable())
					page.open();
				for (Entry<String,Link> e:category.getLinks().entrySet()) {
					categories.put(e.getValue().toString(), e.getKey());
				}
			} catch (IOException e1) {
				e1.printStackTrace();
			}
		}
		return categories;
	}
	public Map<String,String> getSeasons(String link) {
		Map<String,String> seasons = new LinkedHashMap<String,String>();
		SeriesParserStrategy series = null;
		for (int i = 0;i < 10 && (series == null || !series.isParseable());i++) {
			try {
				Page page = new Page(link);
				WelcomeParserStrategy welcome = new WelcomeParserStrategy();
				series = new SeriesParserStrategy();
				page.addParser(welcome);
				page.addParser(series);
				page.open();
				if (welcome.isParseable())
					page.open();
				for (Entry<String,Link> e:series.getLinks().entrySet()) {
					seasons.put(e.getValue().toString(), e.getKey());
				}
			} catch (IOException e1) {
				e1.printStackTrace();
			}
		}
		return seasons;
	}
	public Map<String,String> getEpisodes(String link) {
		Map<String,String> seasons = new LinkedHashMap<String,String>();
		SeasonParserStrategy season = null;
		for (int i = 0;i < 10 && (season == null || !season.isParseable());i++) {
			try {
				Page page = new Page(link);
				WelcomeParserStrategy welcome = new WelcomeParserStrategy();
				season = new SeasonParserStrategy();
				page.addParser(welcome);
				page.addParser(season);
				page.open();
				if (welcome.isParseable())
					page.open();
				for (Entry<String,Link> e:season.getLinks().entrySet()) {
					seasons.put(e.getValue().toString(), e.getKey());
				}
			} catch (IOException e1) {
				e1.printStackTrace();
			}
		}
		return seasons;
	}
	public Map<String,String> getDownloads(CaptchaForm cf) throws WrongCaptchaCodeException {
		Map<String,String> downloads = new LinkedHashMap<String,String>(); 
		try {
			Page page = new Page(cf);
			CaptchaPageParserStrategy captchaStrategy = new CaptchaPageParserStrategy();
			DownloadPageParserStrategy downloadStrategy = new DownloadPageParserStrategy();
			page.addParser(captchaStrategy);
			page.addParser(downloadStrategy);
			page.open();
			if (captchaStrategy.isParseable()) {
				System.out.println("Captcha was wrong");
				throw new WrongCaptchaCodeException(page);
			}
			else {
				for (Entry<String,Link> e:downloadStrategy.getLinks().entrySet()) {
					System.out.println(e.getValue().toString()+" - "+e.getKey());
					downloads.put(e.getValue().toString(), e.getKey());
				}
			}
		} catch (IOException e1) {
			e1.printStackTrace();
		}
		return downloads;
	}
}
